package com.email.crontab.framework.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class EasyPoiUtil {
    /**
     * @param templatePath 模板文件路径
     * @param filePath     导出Excel文件路径
     * @param map          传入需要导出的数据
     * @throws Exception
     */
    public static String templateToExcel(String templatePath, String fileName, Map<String, Object> map) throws Exception {
        try {
            String excelPath = "excel";
            File splits = new File(excelPath);
            if (!splits.exists()) {
                splits.mkdir();
            }
            String filePath = splits.getAbsolutePath();
            TemplateExportParams params = new TemplateExportParams(templatePath);
        /*    List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();//循环写入模板，暂时用不到
            for (int i = 0; i < 4; i++) {
                Map<String, String> lm = new HashMap<String, String>();
                lm.put("id", i + 1 + "");
                listMap.add(lm);
            }
            map.put("maplist", listMap);*/

            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            String workOrderno = DateFormatUtil.getWorkOrderNo();
            fileName = filePath + "\\" + fileName + workOrderno + ".xlsx";
            fileName = fileName.replaceAll("\\\\", "/");

            FileOutputStream fos = new FileOutputStream(fileName);
            workbook.write(fos);
            fos.close();
            return fileName;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param templatePath 模板文件路径
     * @param filePath     导出Excel文件路径
     * @param map          传入需要导出的数据
     * @throws Exception
     */
    public static String exportBigExcel(String fileName, List<LinkedHashMap<String, Object>> list) throws Exception {
        try {
            String excelPath = "excel";
            File splits = new File(excelPath);
            if (!splits.exists()) {
                splits.mkdir();
            }
            String filePath = splits.getAbsolutePath();
            LinkedHashMap<String, Object> stringObjectMap = list.get(0);
            List<ExcelExportEntity> colList = new ArrayList<>();
            for (String s : stringObjectMap.keySet()) {
                ExcelExportEntity colEntity = new ExcelExportEntity(s, s);
//                colEntity.setNeedMerge(true);
                colList.add(colEntity);
            }

            int limit = (list.size() + MAX_NUMBER - 1) / MAX_NUMBER;
            List<List<LinkedHashMap<String, Object>>> mglist = new ArrayList<>();
            Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
                mglist.add(list.stream().skip(i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toList()));
            });
            list.clear();
            Workbook workbook = null;
            int i = 0;
            ExportParams params = new ExportParams(null, fileName);
            for (List<LinkedHashMap<String, Object>> maps : mglist) {
                workbook = ExcelExportUtil.exportBigExcel(params, colList, maps);
                maps.clear();
            }
            ExcelExportUtil.closeExportBigExcel();
            mglist.clear();

            String workOrderno = DateFormatUtil.getWorkOrderNo();
            fileName = filePath + "\\" + fileName + workOrderno + ".xlsx";
            fileName = fileName.replaceAll("\\\\", "/");

            FileOutputStream fos = new FileOutputStream(fileName);
            workbook.write(fos);
            fos.close();
            return fileName;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    //按每10000个一组分割
    private static final Integer MAX_NUMBER = 10000;

    /**
     * 计算切分次数
     */
    private Integer countStep(Integer size) {
        return (size + MAX_NUMBER - 1) / MAX_NUMBER;
    }
}
